#ifndef _CMONTHVIEW_H_
#define _CMONTHVIEW_H_

#include "auto.h"

#include <buola/widgets/cwidget.h>

#include <buola/datetime/cdatetime.h>
#include <buola/utility/usignal.h>

namespace buola { namespace bcal {

class CMonthView : public gui::CWidget
{
public:
    struct SEntry
    {
        CCalendarEvent *mEvent;

        CPoint_d mPos;

        img::CColor mBallColor;
    };

    struct SDay
    {
        std::vector<std::unique_ptr<SEntry>> mEntries;

        CRect_d mRect;
    };

    enum EValid
    {
        VALID_NONE=0,
        VALID_ENTRIES=(1<<0),
        VALID_SHAPE=(1<<1),
        VALID_POSITIONS=(1<<2)
    };

    DECLARE_FRIEND_ENUM_BITOPS(EValid)

    CMonthView(CCalManager *pManager);
    ~CMonthView();

//Managing fields
    void SetDate(const CDate &pDate);
    void SetToday();

    void Next();
    void Previous();

private:
    void Invalidate(EValid pWhat)   {   mValid&=~pWhat; }

    void CalcEntries();
    void CalcShape();
    void CalcPositions();

    void MakeValid(EValid pWhat);


public:
////////////// Event handling //////////////
    virtual void OnPreExpose();
    virtual void OnExpose(const gui::CExposeEvent &pE);
    virtual void OnButtonDown(const gui::CMouseEvent &pE);
    virtual void OnButtonUp(const gui::CMouseEvent &pE);
    virtual void OnMouseMove(const gui::CMouseEvent &pE);
    virtual void OnSize(const gui::CGeometryEvent &pE);

    virtual void OnStyleChanged(TStyle);

    void OnCalendarAdded(CCalendar *pCalendar);
    void OnCalendarRemoved(CCalendar *pCalendar);

protected:
/////////////// protected functions //////////////

public:
    USignal<void(const std::wstring&)> sChangeTitle;

private:
    img::PFont mFont;

    CDate mDate;
    CDate mFirstDate;

    int mRows;
    CRect_d mMonthsRect;

    CCalManager *mManager;

    EValid mValid;

    std::vector<std::unique_ptr<SDay>> mDays;
};

/*namespace buola*/ } /*namespace bcal*/ }

#endif
